package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class DispatcherServlet extends HttpServlet {

    protected Map<String,BaseServlet> maps=new HashMap<>();

    @Override
    public void init() throws ServletException {
        maps.put("/user",new UserServlet());
        maps.put("goods",new GoodsServlet());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String configName = request.getParameter("configName");
        System.out.println(configName);


        String url = request.getRequestURI();
        url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));

        BaseServlet baseServlet = maps.get(url);
        try{
            baseServlet.execute(request, response);
        }catch (InvocationTargetException e){
            throw new  RuntimeException(e);
        }catch (IllegalAccessException e){

            throw new  RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
